package com.atlin.provider;

import com.atlin.nrpc.core.RpcApplication;
import com.atlin.nrpc.core.config.RegistryConfig;
import com.atlin.nrpc.core.config.RpcConfig;
import com.atlin.nrpc.core.model.ServiceMetaInfo;
import com.atlin.nrpc.core.registry.LocalRegistry;
import com.atlin.nrpc.core.registry.Registry;
import com.atlin.nrpc.core.registry.RegistryFactory;
import com.atlin.nrpc.core.service.VertxHttpServer;
import com.atlin.service.UserInfoService;

/**
 * 服务提供者示例
 */
public class ProviderExample {

    public static void main(String[] args) {
        RpcApplication.init();

        String serviceName = UserInfoService.class.getName();
        LocalRegistry.register(serviceName, UserInfoServiceImpl.class);

        // 将服务注册到注册中心

        RpcConfig rpcConfig = RpcApplication.getRpcConfig();
        RegistryConfig registryConfig = rpcConfig.getRegistryConfig();
        Registry registry = RegistryFactory.getInstance(registryConfig.getRegistry());

        ServiceMetaInfo serviceMetaInfo = new ServiceMetaInfo();
        serviceMetaInfo.setServiceName(serviceName);
        serviceMetaInfo.setServiceHost(rpcConfig.getServerHost());
        serviceMetaInfo.setServicePort(rpcConfig.getServerPort());
        try {
            registry.registry(serviceMetaInfo);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        VertxHttpServer httpServer = new VertxHttpServer();
        httpServer.doStart(RpcApplication.getRpcConfig().getServerPort());

    }
}
